package org.birrap.ejb;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.birrap.entities.BaresT;


@Stateless
public class BaresTBean implements BaresTBeanLocal {

    @PersistenceContext(unitName = "BirrApp-ejbPU")
    private EntityManager em;
    
    
    @Override
    public List<BaresT> findAll() 
    {
        Query query = em.createNamedQuery("BaresT.findAll");
        
        return (List<BaresT>) query.getResultList();
    }
    

    @Override
    public BaresT findByIdBar(Integer idBar) 
    {
        BaresT res = null;
        
        Query query = em.createNamedQuery("BaresT.findByIdBar")
                        .setParameter("idBar", idBar);
        
        try 
        {
            res = (BaresT) query.getSingleResult();
        } 
        catch(NoResultException e) 
        {
            System.out.println("No existe el id de bar");
        }
        
        return res; 
    }

    @Override
    public List<BaresT> findByNombre(String nombre) {
        
        List<BaresT> res = null;
        
        Query query = em.createNamedQuery("BaresT.findByNombre")
                        .setParameter("nombre", nombre);
        
        try 
        {
            res = (List<BaresT>) query.getResultList();
        } 
        catch(NoResultException e) 
        {
            System.out.println("No existe el nombre de bar");
        }
        
        return res;
    }

    @Override
    public BaresT findByDireccion(String direccion) {
        
        BaresT res = null;
        
        Query query = em.createNamedQuery("BaresT.findByDireccion")
                        .setParameter("direccion", direccion);
        
        try 
        {
            res = (BaresT) query.getSingleResult();
        } 
        catch(NoResultException e) 
        {
            System.out.println("No existe la direccion");
        }
        
        return res; 
    }

    @Override
    public BaresT findByTelefono(Integer telefono) {
        
        BaresT res = null;
        
        Query query = em.createNamedQuery("BaresT.findByTelefono")
                        .setParameter("telefono", telefono);
        
        try 
        {
            res = (BaresT) query.getSingleResult();
        } 
        catch(NoResultException e) 
        {
            System.out.println("No existe el telefono");
        }
        
        return res; 
    }

    @Override
    public List<BaresT> findByCodigoPostal(Integer codigo) {
        
        List<BaresT> res = null;
        
        Query query = em.createNamedQuery("BaresT.findByCodigoPostal")
                        .setParameter("codigo", codigo);
        
        try
        {
            res = (List<BaresT>) query.getResultList();
        }
        catch(NoResultException e) 
        {
            System.out.println("No existe el codigo de bar");
        }
        
        return res;
    }

    @Override
    public BaresT insert(BaresT bar) {
    
        return update(bar);
    }

    @Override
    public BaresT update(BaresT bar) {
        return em.merge(bar);
    }

    @Override
    public List<BaresT> findBaresConCervezas() {
        
          
        List<BaresT> res = null;
        
        Query query = em.createNamedQuery("BaresT.findBaresConCervezas");
                        
        
        try
        {
            res = (List<BaresT>) query.getResultList();
        }
        catch(NoResultException e) 
        {
            System.out.println("No existe bares sin cervezas");
        }
        
        return res;
    }
    
        
    @Override
    public List<BaresT> executeSql(String sql)
    {
        List<BaresT> listaResul = 
                (List<BaresT>)em.createNativeQuery(sql, BaresT.class)
                                .getResultList();
        
        return listaResul;
    }
    
    @Override
    public boolean remove(Integer idBar) {
        
        boolean correcto = false;
        int resultado = 0;
        
        Query query = em.createNamedQuery("BaresT.removeByIdBar")
                         .setParameter("idBar", idBar);
        
        resultado = query.executeUpdate();
        
        if (resultado > 0)
        {
            correcto = true;
        }
        
        return correcto;
    }
    @Override
    public void refresh(BaresT bar){
        
        em.refresh(bar);
    }
}
